(function (window, $, undefined) {
var _block_namespaces_ = window._block_namespaces_ || (window._block_namespaces_ = {});
var name = "block27004";
var block27004 = _block_namespaces_[name] || (_block_namespaces_[name] = {});
var options = {}
$.extend(block27004, {
"init": init,
"module_init": module_init,
})
function init(nodeObj) {
if (!!!nodeObj) {
return;
}
$.extend(options, nodeObj)
options.$selector = $('div[data-settingid=' + options.settingId + ']');
if (options.state == 'frontend') {
options.prodId = $('#productInfo input[name=id]').val() || ''
} else {
var grandParentDocument = window.parent.parent.document;
var element = $(grandParentDocument).find('#product\\.encodePkId');
options.prodId = element.val() || ''
}
//默认初始化 插件
fetchVideoList()
$(window).resize(throttle(function () {
setVideoItemCoverSize();
}, 250))
}
//模块修改时调用该方法
function module_init() {
setVideoItemCoverSize()
}
function fetchVideoList(page) {
var ajaxConfig = {
url: '/phoenix2/composite/product/related/video/list/info',
method: 'get',
data: {
page: page || 1,
prodId: options.prodId,
limit: 8,
},
success: function (res) {
if (res.code == 200000 && res.data && res.data.list && res.data.list.length > 0) {
formatListDom(res.data.list)
setVideoItemCoverSize()
if (options.$selector.find('.block_27004_video_pagination .b_27004_p_content .b_b66_page_item').length == 0) {
let obj = {
total: res.data.totalRow,
currentPage: res.data.totalPage,
}
setPagination(obj)
}
} else {
options.$selector.find('.block_27004_video_list_content').html('')
options.$selector.find('.block_27004_video_list_content').hide();
options.$selector.find('.block_27004_no_video_data').css('display', 'flex');
}
},
error: function () {
options.$selector.find('.block_27004_video_list_content').html('')
options.$selector.find('.block_27004_video_list_content').hide();
options.$selector.find('.block_27004_no_video_data').css('display', 'flex');
}
}
if (options.state == 'frontend') {
ajaxConfig.beforeSend = function (xhr) {
xhr.setRequestHeader('anony', 'Y');
}
} else {
var authorization = ''
if (window.parent && window.parent.location.search && parseUrlParams(window.parent.location.search)) {
authorization = parseUrlParams(window.parent.location.search)['Authorization'];
}
ajaxConfig.beforeSend = function (xhr) {
xhr.setRequestHeader('Authorization', authorization);
}
}
$.ajax(ajaxConfig)
}
function parseUrlParams(url) {
const params = {};
const urlParts = url.split('?');
if (urlParts.length > 1) {
const queryString = urlParts[1];
const paramPairs = queryString.split('&');
paramPairs.forEach(pair => {
const [key, value] = pair.split('=');
params[key] = decodeURIComponent(value);
});
}
return params;
}
function setPagination(pageData) {
var totalPages = Math.ceil(pageData.total / 8);
if (totalPages <= 1) { return }
var html = ''
for (let i = 0; i < totalPages; i++) {
var cur = i + 1;
var temp = '
' + cur + '
'
if (cur == pageData.currentPage) {
temp = '' + cur + '
'
}
html += temp
}
options.$selector.find('.block_27004_video_pagination .b_27004_p_content').html(html)
options.$selector.find('.block_27004_video_pagination').show();
var currentPage = pageData.currentPage;
var pageReturnFlag = false
options.$selector.find('.block_27004_video_pagination .b_b66_page_item').on('click', function () {
var pageInfo = $(this).attr('data-page')
if ($(this).hasClass('page_active')) {
return
}
if (pageInfo == 'pre') {
currentPage--
pageReturnFlag = currentPage <= 0 ? true : false
currentPage = Math.max(1, currentPage);
} else if (pageInfo == 'next') {
currentPage++
pageReturnFlag = currentPage > totalPages ? true : false
currentPage = Math.min(totalPages, currentPage)
} else {
pageReturnFlag = false
currentPage = pageInfo
}
if (pageReturnFlag) return
options.$selector.find('.block_27004_video_pagination .b_27004_p_content .b_b66_page_item').removeClass('page_active');
options.$selector.find('.block_27004_video_pagination .b_27004_p_content .b_b66_page_item').each(function () {
if (currentPage == $(this).attr('data-page')) {
$(this).addClass('page_active')
}
})
fetchVideoList(currentPage)
})
}
function setVideoItemCoverSize() {
if (options.$selector.find('.block_27004_video_item').length > 0) {
var blockWidth = options.$selector.find('.block27004').width();
if (blockWidth > 0 && blockWidth <= 800) {
options.$selector.find('.block_27004_video_item').css('width', 'calc(50% - 10px)');
} else if (blockWidth > 800) {
options.$selector.find('.block_27004_video_item').css('width', 'calc(25% - 10px)');
}
var itemWidth = options.$selector.find('.block_27004_video_item:first-of-type').width();
if (itemWidth <= 0) {
return
}
var itemHeight = itemWidth * (9 / 16);
options.$selector.find('.block_27004_video_item .block_27004_v_cover').height(itemHeight)
var playIconSize = (itemWidth * 0.1 / 32).toFixed(2) < 1 ? 1 : (itemWidth * 0.1 / 32).toFixed(2);
options.$selector.find('.block_27004_video_item .block_27004_v_cover .block_27004_v_play_icon').css('transform', 'translate(-50%, -50%) scale(' + playIconSize + ')')
}
}
function handleClickOutside(event) {
event.stopPropagation();
if (event.target.className == 'block_27004_video_detail_popup') {
options.$selector.find('.block_27004_video_detail_popup').hide()
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_left').html('')
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_right').html('')
options.$selector.find('.block_27004_video_detail_popup').off('click', handleClickOutside)
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_close').off('click', bindClosePopup)
}
};
function setHtmlAndBind(data) {
var leftHtml = '';
var rightHtml = '';
if (data.isThirdParty) {
leftHtml = ''
} else {
var isM3u8Video = Boolean(data.video_url.indexOf('mediaType=m3u8') > -1);
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
leftHtml = '';
} else if (isM3u8Video) {
leftHtml = '';
} else {
leftHtml = '';
}
}
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_left').html(leftHtml)
var prodHtml = '';
for (var i = 0; i < data.prods.length; i++) {
var item = data.prods[i];
var priceDom = '';
var tipText = '';
if (item.newPrice) {
tipText = options.$selector.find('#shopnow').text();
if (item.oldPrice) {
priceDom = '' + item.unit + '
' + item.newPrice + '
' + item.unit + '
' + item.oldPrice + '
';
} else {
priceDom = '' + item.unit + '
' + item.newPrice + '
';
}
} else {
tipText = options.$selector.find('#inquire').text();
}
var prodSrc = item.prodUrl ? item.prodUrl : 'javascript:void(0);'
var tempHtml = '' + item.name + '
' + priceDom + '
';
prodHtml += tempHtml;
}
var descDom = data.description ? '' + data.description + '
' : ''
rightHtml = '' + data.title + '
' + descDom + '
' + prodHtml + '
';
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_right').html(rightHtml)
// 此处处理币种切换之后的价格变化
if (phoenixSite.payCoinExchange && phoenixSite.payCoinExchange.handleElement) {
phoenixSite.payCoinExchange.handleElement(options.$selector.find('.block_27004_video_detail_popup'));
}
options.$selector.find('.block_27004_video_detail_popup').show()
options.$selector.find('.block_27004_video_detail_popup').on('click', handleClickOutside)
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_close').on('click', bindClosePopup)
}
function bindClosePopup() {
options.$selector.find('.block_27004_video_detail_popup').hide()
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_left').html('')
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_right').html('')
options.$selector.find('.block_27004_video_detail_popup').off('click', handleClickOutside)
options.$selector.find('.block_27004_video_detail_popup .block_27004_video_detail_content .block_27004_video_detail_content_close').off('click', bindClosePopup)
}
function formatProdListData(resData) {
var prodData = []
for (var i = 0; i < resData.products.length; i++) {
var item = resData.products[i];
var obj = {
img: item.photoUrlNormal,
name: item.prodName,
prodUrl: item.prodUrl,
newPrice: '',
oldPrice: '',
unit: '',
}
if (resData.funcPayAuth && (item.isProdTradeEnabled || (resData.solutionName && resData.solutionName == 'b2c'))) {
if (item.isSkuProd && item.isSkuProd == '1') {
if (item.shopProdPrice != item.shopProdPriceMax) {
obj.unit = resData.currencySymbol;
obj.newPrice = item.shopProdPrice;
obj.oldPrice = item.shopProdPriceMax;
} else {
obj.unit = resData.currencySymbol;
obj.newPrice = item.shopProdPrice;
obj.oldPrice = '';
}
} else {
if (item.prodDiscountPrice && item.prodDiscountPrice != item.prodPrice) {
obj.unit = resData.currencySymbol;
obj.newPrice = item.prodDiscountPrice;
obj.oldPrice = item.prodPrice;
} else {
obj.unit = resData.currencySymbol;
obj.newPrice = item.prodPrice;
obj.oldPrice = '';
}
}
} else {
obj.unit = '';
obj.newPrice = '';
obj.oldPrice = '';
}
prodData.push(obj);
}
return prodData
}
function formatListDom(list) {
var html = ''
for (var index = 0; index < list.length; index++) {
var item = list[index];
var tempHtml = '' +
'
' +
'
' +
'
' +
'
' +
'
' + item.title + '
' +
'
' + item.description + '
' +
'
';
html += tempHtml
}
options.$selector.find('.block_27004_video_list_content').html(html)
options.$selector.find('.block_27004_video_list_content').show();
options.$selector.find('.block_27004_no_video_data').hide();
if (options.state == 'frontend') {
options.$selector.find('.block_27004_v_cover').on('click', function () {
var index = $(this).attr('data-index');
var videoId = $(this).attr('data-video-id');
var thirdType = $(this).attr('data-third-type') == 'true' ? true : false;
var videoSrc = $(this).attr('data-video-src');
var description = list[index].description;
var title = list[index].title;
$.ajax({
url: '/phoenix/admin/videoExpand/getDetail',
data: { videoId: videoId },
method: 'get',
success: function (res) {
var list = formatProdListData(JSON.parse(res))
var temp = {
prods: list,
isThirdParty: thirdType,
video_url: videoSrc,
description: description,
title: title,
}
setHtmlAndBind(temp)
}
})
})
}
}
function throttle(fn, delay) {
var timer = null;
return function () {
if (!timer) {
timer = setTimeout(function () {
fn.apply(this, arguments);
timer = null;
}, delay);
}
};
}
})(window, jQuery);